package com.lzy.cloud.payment.controller;

import com.lzy.cloud.payment.constants.BusinessStatus;
import com.lzy.cloud.payment.constants.CommonConstants;
import com.lzy.cloud.payment.dtos.pay.BasePayDTO;
import com.lzy.cloud.payment.dtos.pay.PayBackDTO;
import com.lzy.cloud.payment.dtos.payment.PayInfoDetailDto;
import com.lzy.cloud.payment.dtos.payment.PayInfoDto;
import com.lzy.cloud.payment.facade.PayTradeFacade;
import com.lzy.cloud.payment.remote.AccountApi;
import com.lzy.cloud.payment.remote.BaseApi;
import com.lzy.cloud.payment.remote.OrderApi;
import com.lzy.cloud.payment.remote.dtos.AccountWalletDto;
import com.lzy.cloud.payment.remote.dtos.AccountWalletInfoDto;
import com.lzy.cloud.payment.remote.dtos.OrderExpenseItemDto;
import com.lzy.cloud.payment.remote.request.AccountWalletInfoRequest;
import com.lzy.cloud.payment.remote.request.OrderExpenseItemRequest;
import com.lzy.cloud.payment.service.PayInfoDetailService;
import com.lzy.cloud.payment.service.PayInfoService;
import com.lzy.cloud.payment.utils.CommonsUtil;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.framework.tools.commons.BigDecimalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 * 支付交易业务处理
 */
@RestController
@Slf4j
@RequestMapping("pay/trade")
public class PayTradeController {
    Logger logger = Logger.getLogger(PayTradeController.class);

    @Autowired
    PayTradeFacade payTradeFacade;

    @Autowired
    PayInfoService payInfoService;

    @Autowired
    PayInfoDetailService payInfoDetailService;

    @Autowired
    OrderApi orderApi;

    @Autowired
    AccountApi accountApi;

    @Autowired
    BaseApi baseApi;

    @RequestMapping(value = "notify",method = RequestMethod.POST)
    public Result notify(@RequestBody PayBackDTO payBackDTO){
        Map<String,String> params = payBackDTO.getParams();
        BasePayDTO basePayDTO = payBackDTO.getBasePayDTO();
        switch (basePayDTO.getWay()){
            case BusinessStatus.PayWay.alipay:
                if(params.get("trade_status").equals("TRADE_FINISHED") || params.get("trade_status").equals("TRADE_SUCCESS")){
                    //支付成功
                    boolean flag = payTradeFacade.notifyProcess(basePayDTO);
                    if(flag){
                        return new Result(CommonConstants.SUCCESS);
                    }
                }else{
                    //支付失败，解冻
                    Boolean ok = DozerUtils.map(orderApi.cancelPayService(basePayDTO.getOut_trade_no()).getResult(),Boolean.class);
                    if(ok){
                        return new Result(CommonConstants.SUCCESS);
                    }
                }
            case BusinessStatus.PayWay.weixin:
                if(params.get("result_code").equals("SUCCESS")){
                    //支付成功
                    Boolean flag = payTradeFacade.notifyProcess(basePayDTO);
                    if(flag){
                        return new Result(CommonConstants.SUCCESS);
                    }
                }else{
                    //支付失败，解冻
                    Boolean ok = DozerUtils.map(orderApi.cancelPayService(basePayDTO.getOut_trade_no()).getResult(),Boolean.class);
                    if(ok){
                        return new Result(CommonConstants.SUCCESS);
                    }
                }
        }
        return new Result(CommonConstants.FAIL);
    }
}
